# coding:utf-8

import paramiko
import base64

class TapSSHClient(object):

    def __init__(self, site, username, password, rsa_key):
        self.site = site
        self.username = username
        self.password = password
        self.rsa_key = rsa_key
        self.sshclient = self.create_ssh_client()

    def create_ssh_client(self):
        sc = paramiko.SSHClient()
        key = paramiko.RSAKey(data=base64.b64decode(self.rsa_key))
        sc.get_host_keys().add(self.site, 'ssh-rsa', key)
        sc.connect(self.site, username = self.username, password = self.password)
        return sc

    def execute_command(self, cmd):
        stdin, stdout, stderr = self.sshclient.exec_command(cmd)
        return (stdout, stderr)

    def close(self):
        self.sshclient.close()